package cn.hx.admin.controller;

import cn.hx.admin.entity.User;
import cn.hx.admin.entity.po.UserPo;
import cn.hx.admin.service.UserService;

import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户信息表(User)表控制层
 *
 * @author makejava
 * @since 2023-12-10 21:19:11
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 分页查询
     *
     * @param user 筛选条件
     * @return 查询结果
     */
    @GetMapping
    @PreAuthorize("hasAuthority('system:user:list')")
    public ResponseEntity<List<User>> queryByPage(User user) {
        return ResponseEntity.ok(this.userService.queryByPage(user));
    }

    /**
     * 查打卡和部门
     *
     * @param user
     * @return
     */

    @GetMapping("/toDeptAndUserSign")
    @PreAuthorize("hasAuthority('system:user:sign')")
    public ResponseEntity<List<User>> queryByDeptAndUserSign(User user) {
        return ResponseEntity.ok(this.userService.queryByDeptAndUserSign(user));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @PreAuthorize("hasAuthority('system:user:query')")
    public ResponseEntity<User> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.userService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param user 实体
     * @return 新增结果
     */
    @PostMapping
    @PreAuthorize("hasAuthority('system:user:add')")
    public ResponseEntity<User> add(@RequestBody User user) {
        System.out.println(user);
        return ResponseEntity.ok(this.userService.insert(user));
    }

    /**
     * 编辑数据
     *
     * @param userPo 实体
     * @return 编辑结果
     */
    @PutMapping
    @PreAuthorize("hasAuthority('system:user:edit')")
    public ResponseEntity<User> edit(@RequestBody UserPo userPo) {
        User user = new User();
        BeanUtils.copyProperties(userPo, user);
        return ResponseEntity.ok(this.userService.update(user));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    @PreAuthorize("hasAuthority('system:user:remove')")
    public ResponseEntity<Boolean> deleteById(Integer id) {
        System.err.println(id);
        return ResponseEntity.ok(this.userService.deleteById(id));
    }

    /**
     * 批量逻辑删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("deleteUserByIdsBatch")
    @PreAuthorize("hasAuthority('system:user:remove')")
    public ResponseEntity<Boolean> deleteUserByIdsBatch(String ids) {
        return ResponseEntity.ok(this.userService.deleteUserByIdsBatch(ids));
    }

}

